package com.zhao.service.search;

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class LuceneService {

	protected static final Logger logger = LoggerFactory.getLogger(LuceneService.class);

	public static String LUCENE_DIRECTORY = "lucene_directory";

	public static String LUCENE_PATH = "lucene_path";

	private Directory directory;

	private IndexReader indexReader;

	private IndexWriter indexWriter;

	private IndexSearcher indexSearcher;

	public void searchIndex(String fieldName) throws Exception {
		if (indexReader == null) {
			initIndexSearcher();
		}
		for (int i = 0; i < indexReader.maxDoc(); i++) {
			Document doc = indexSearcher.doc(i);
			IndexableField field = doc.getField(fieldName);
			logger.debug(field.stringValue());
		}
	}

	public void initDirectory() throws Exception {
		Path path = FileSystems.getDefault().getPath(LUCENE_PATH, LUCENE_DIRECTORY);
		Directory directory = FSDirectory.open(path);
		setDirectory(directory);
	}

	public void initIndexWriter() throws Exception {
		if (directory == null) {
			initDirectory();
		}
		Analyzer analyzer = new MyAnalyzer();
		IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
		IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
		setIndexWriter(indexWriter);
	}

	public void initIndexReader() throws Exception {
		if (directory == null) {
			initDirectory();
		}
		setIndexReader(indexReader);
	}

	public void initIndexSearcher() throws Exception {
		if (indexReader == null) {
			initIndexReader();
		}
		ExecutorService executor = Executors.newCachedThreadPool();
		IndexSearcher indexSearcher = new IndexSearcher(indexReader, executor);
		this.indexSearcher = indexSearcher;
	}

	public void addIndex(String name, String value) throws Exception {
		logger.debug("name:" + name + "| value:" + value);
		if (indexWriter == null) {
			initIndexWriter();
		}
		Document document = new Document();
		document.add(new StringField(name, value, Store.YES));
		indexWriter.addDocument(document);
		indexWriter.close();
	}

	public Directory getDirectory() {
		return directory;
	}

	public void setDirectory(Directory directory) {
		this.directory = directory;
	}

	public IndexReader getIndexReader() {
		return indexReader;
	}

	public void setIndexReader(IndexReader indexReader) {
		this.indexReader = indexReader;
	}

	public IndexWriter getIndexWriter() {
		return indexWriter;
	}

	public void setIndexWriter(IndexWriter indexWriter) {
		this.indexWriter = indexWriter;
	}
}